package com.yanqu.road.dao.impl.activity.dafuweng;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dafuweng.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DFWUserDataDaoImpl extends TempDataObjectDao<DFWUserData> {

    public static String getDFWUserDataSql = "select * from t_u_dfw_user_data where activityId=?";

    public Map<Long, DFWUserData> getDFWUserDataMap(int activityId) {
        Map<Long, DFWUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDFWUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWUserData dFWUserData = parse(rs);
                map.put(dFWUserData.getUserId(), dFWUserData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateDFWUserDataSql = "update t_u_dfw_user_data set goldScore=?,positionIndex=?,dollId=?,vehicleId=?,vehicleMoveTimes=?,energy=?,lastRecoveryTime=?,complete=?,freeBattleTimes=?,dollList=?,yongBingTimes=?,userSecretRoadList=?,zaoYaoOptionalList=?,rewardCollectionMap=?,luZhangDataList=?,eventId=?,expandParam=?,moneyMallList=?,rebateRewardRecord=?,boxOpenTimes=?,lastResetTime=?,dfwGodDataList = ?,totalDamage=?,packageList = ? where activityId=? and userId=?";

    @Override
    protected boolean updateData(DFWUserData dFWUserData) {
        dFWUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getGoldScore()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getPositionIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getDollId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getVehicleId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getVehicleMoveTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, dFWUserData.isComplete()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getFreeBattleTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getDollList())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getYongBingTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getUserSecretRoadList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getZaoYaoOptionalList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getRewardCollectionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getLuZhangDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dFWUserData.getExpandParam()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getMoneyMallList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getRebateRewardRecord())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getBoxOpenTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUserData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getDfwGodDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getTotalDamage()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSONObject.toJSONString(dFWUserData.getPackageList())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUserData.getUserId()));
        if (getDb().executeNoneQuery(updateDFWUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDFWUserDataSql = "insert into t_u_dfw_user_data(userId,activityId,goldScore,positionIndex,dollId,vehicleId,vehicleMoveTimes,energy,lastRecoveryTime,complete,freeBattleTimes,dollList,yongBingTimes,userSecretRoadList,zaoYaoOptionalList,rewardCollectionMap,luZhangDataList,eventId,expandParam,moneyMallList,rebateRewardRecord,boxOpenTimes,lastResetTime,dfwGodDataList,totalDamage,packageList) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DFWUserData dFWUserData) {
        dFWUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dFWUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getGoldScore()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getPositionIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getDollId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getVehicleId()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getVehicleMoveTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUserData.getLastRecoveryTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, dFWUserData.isComplete()));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getFreeBattleTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getDollList())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getYongBingTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getUserSecretRoadList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getZaoYaoOptionalList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getRewardCollectionMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getLuZhangDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dFWUserData.getExpandParam()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getMoneyMallList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getRebateRewardRecord())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getBoxOpenTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, dFWUserData.getLastResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(dFWUserData.getDfwGodDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, dFWUserData.getTotalDamage()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSONObject.toJSONString(dFWUserData.getPackageList())));
        if (getDb().executeNoneQuery(insertDFWUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DFWUserData parseData(ResultSet rs) throws SQLException {
        DFWUserData dFWUserData = new DFWUserData();
        dFWUserData.setUserId(rs.getLong("userId"));
        dFWUserData.setActivityId(rs.getInt("activityId"));
        dFWUserData.setGoldScore(rs.getInt("goldScore"));
        dFWUserData.setPositionIndex(rs.getInt("positionIndex"));
        dFWUserData.setDollId(rs.getInt("dollId"));
        dFWUserData.setVehicleId(rs.getInt("vehicleId"));
        dFWUserData.setVehicleMoveTimes(rs.getInt("vehicleMoveTimes"));
        dFWUserData.setEnergy(rs.getInt("energy"));
        dFWUserData.setLastRecoveryTime(rs.getLong("lastRecoveryTime"));
        dFWUserData.setComplete(rs.getBoolean("complete"));
        dFWUserData.setFreeBattleTimes(rs.getInt("freeBattleTimes"));
        dFWUserData.setDollList(JSONObject.parseObject(rs.getString("dollList"), new TypeReference<List<DFWDollData>>() {
        }));
        dFWUserData.setYongBingTimes(rs.getInt("yongBingTimes"));
        dFWUserData.setBoxOpenTimes(rs.getInt("boxOpenTimes"));
        dFWUserData.setUserSecretRoadList(JSONObject.parseObject(rs.getString("userSecretRoadList"), new TypeReference<List<DFWUserSecretRoad>>() {
        }));
        dFWUserData.setZaoYaoOptionalList(JSONObject.parseObject(rs.getString("zaoYaoOptionalList"), new TypeReference<List<DFWZaoYaoOptional>>() {
        }));
        dFWUserData.setRewardCollectionMap(JSONObject.parseObject(rs.getString("rewardCollectionMap"), new TypeReference<Map<Integer, Integer>>() {
        }));
        dFWUserData.setLuZhangDataList(JSONObject.parseObject(rs.getString("luZhangDataList"), new TypeReference<List<LuZhangData>>() {
        }));
        dFWUserData.setEventId(rs.getInt("eventId"));
        dFWUserData.setExpandParam(rs.getString("expandParam"));
        dFWUserData.setMoneyMallList(JSONObject.parseObject(rs.getString("moneyMallList"), new TypeReference<List<Integer>>() {
        }));
        dFWUserData.setRebateRewardRecord(JSONObject.parseObject(rs.getString("rebateRewardRecord"), new TypeReference<List<Integer>>() {
        }));
        dFWUserData.setDfwGodDataList(JSONObject.parseObject(rs.getString("dfwGodDataList"), new TypeReference<List<DFWGodData>>() {
        }));
        dFWUserData.setLastResetTime(rs.getLong("lastResetTime"));
        dFWUserData.setTotalDamage(rs.getInt("totalDamage"));
        String packageList = rs.getString("packageList");
        if (!StringUtils.isNullOrEmpty(packageList)) {
            List<DfwPackageData> dfwPackageData = JSONObject.parseObject(packageList, new TypeReference<List<DfwPackageData>>() {
            });
            dFWUserData.setPackageList(dfwPackageData);
        }
        return dFWUserData;
    }


}
